package com.interfaces;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.entity.Customer;

/**
 * @Title: CustomerDao.java
 * @Package com.interfaces
 * @author 孙梦楠
 * @date 2017年7月19日 下午3:50:54
 * @version V1.0.0
 */

public interface CustomerDao extends JpaRepository<Customer, Integer> {
	@Transactional
	@Query(value = "SELECT c FROM Customer c WHERE (c.customeraccount=?1 OR ?1 IS NULL )"
			+ "AND (c.customername like %?2% OR ?2 IS NULL )" + "AND (c.customeraddress like %?3% OR ?3 IS NULL)"
			+ "AND (c.customertype=?4 OR ?4 IS NULL)" + "AND (c.customerorg=?5 OR ?5 IS NULL)"
			+ "AND (c.customerstatus=?6 OR ?6 IS NULL)" + "AND (c.customerphone=?7 OR ?7 IS NULL)"
			+ "AND (c.customerwechat=?8 OR ?8 IS NULL) AND (c.customerrechargestatus !=4)")
	public List<Customer> findByConditions(String customeraccount, String customername, String customeraddress,
			String customertype, Integer customerorg, String customerstatus, String customerphone,
			String customerwechat);

	List<Customer> findByCustomerorgAndCustomerrechargestatusNot(Integer customerorg, String customerrechargestatus);

	List<Customer> findByCustomerorgAndCustomerstatus(Integer customerorg, String customerstatus);

	List<Customer> findByCustomerorgAndCustomertypeAndCustomerstatusAndCustomerrechargestatus(Integer customerorg,
			String customertype, String customerstatus, String customerrechargestatus);

	List<Customer> findByCustomerorgAndCustomertypeAndCustomerstatus(Integer customerorg, String customertype,
			String customerstatus);

	List<Customer> findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(Integer customerorg,
			String customertype, String customerrechargestatus);

	List<Customer> findByCustomertypeAndCustomerrechargestatusNot(String customertype, String customerrechargestatus);

	@Transactional
	@Query(value = "SELECT c FROM Customer c WHERE (c.customeraccount=?1 OR ?1 IS NULL )"
			+ "AND (c.customername like %?2% OR ?2 IS NULL )" + "AND (c.customeraddress like %?3% OR ?3 IS NULL)"
			+ "AND (c.customerorg=?4)" + "AND (c.customertype=?5)" + "AND (c.customerstatus=?6)"
			+ "AND (c.customerrechargestatus=?7 OR ?7 IS NULL)")
	public List<Customer> findByConditions_recharge(String customeraccount, String customername, String customeraddress,
			Integer customerorg, String customertype, String customerstatus, String customerrechargestatus);

	// 更新用户状态
	@Transactional
	@Modifying
	@Query("update Customer c set c.customerstatus = ?1 where c.customerid = ?2")
	int updateCustomer(String customerstatus, Integer customerid);

	// 更新用户开户状态
	@Transactional
	@Modifying
	@Query("update Customer c set c.customerrechargestatus = ?1 where c.customerid = ?2")
	int updateCustomer_recharge(String customerrechargestatus, Integer customerid);

	// 更新用户户号
	@Transactional
	@Modifying
	@Query("update Customer c set c.customeraccount = ?1 where c.customerid = ?2")
	int updateCustomeraccount(String customeraccount, Integer customerid);

	// 更新用户户号
	@Transactional
	@Modifying
	@Query("update Customer c set c.customername = ?1 ,c.customerphone = ?2 ,c.customerwechat = ?3 where c.customerid = ?2")
	int updateCustomernameAndphoneAndwechat(String customername, String customerphone,String customerwechat,Integer customerid);

	// 按照用户编号降序查询
	@Transactional
	@Query("SELECT c FROM Customer c WHERE c.customeraccount like ?1% AND c.customerorg=?2 AND c.customertype=?3 ORDER BY c.customeraccount DESC")
	List<Customer> findByCustomeraccountLikeDESC(String customeraccount, Integer customerorg, String customertype);

}
